import config
import re
from bubbles.randomWords import RandomWords
from utils import *


def register():
	return SearchWord()


class SearchWord:
	def __init__(self):
		self.type = 'passive'
		self.rand = RandomWords()
		self.help = \
'''查询单词：
	用法：*查单词 (单词)
	查询英文单词的意思。'''

	def run(self, gum, msg):
		if not msg['type'] in ('FriendMessage', 'GroupMessage') \
				or len(msg['messageChain']) <= 1 \
				or msg['messageChain'][1]['type'] != 'Plain':
			return


		splitted = msg['messageChain'][1]['text'].split()
		if splitted[0] == '*查单词' and len(splitted) > 1 and re.match('^[A-Za-z0-9_\- ]*$', ' '.join(splitted[1:])):
			word = ' '.join(splitted[1:])
			chain = MsgChain()
			for line in self.rand.searchWord(word):
				chain.addPlain(line + '\n')

			autoReply(chain, gum)